package com.mozz.finally_work.servlet.api;

import com.mozz.finally_work.dao.CategoryDao;
import com.mozz.finally_work.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.UUID;

@WebServlet(name = "AddCategoryServlet", value = "/AddCategoryServlet")
public class AddCategoryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*获取参数*/
        String userId = request.getParameter("userId");
        String categoryName = request.getParameter("categoryName");
        /*调用dao查询*/
        SqlSession session = MybatisUtils.getSession();
        CategoryDao dao = session.getMapper(CategoryDao.class);
        Integer result = dao.addCategory(UUID.randomUUID().toString(), userId, categoryName);
        session.commit();
        session.close();
        response.setHeader("Content-Type", "application/json; charset=UTF-8");
        if (result > 0) {
            response.getWriter().write("success");
        }else{
            response.getWriter().write("false");
        }
    }
}
